﻿using System;
using System.Net.Mail;
using System.Text;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.FileExpress.Controls
{
    public partial class Attachment : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hlSignIn.NavigateUrl = "~/SignIn.aspx?ReturnUrl=" + Server.UrlEncode(Request.RawUrl);
                btnFeedback.Visible = PortalSecurity.CurrentUser.Identity.IsAuthenticated;
                pnlSignIn.Visible = !btnFeedback.Visible;

                Components.Package package = Components.Package.Load(this.ItemID);
                if (package != null)
                {
                    lblTitle.Text = package.Title;
                    lblCreatedBy.Text = package.CreatedBy;
                    lblCreationTime.Text = package.CreationTime.ToString();
                    lblDescription.Text = Server.HtmlDecode(package.Description);
                }
            }
        }

        protected void btnSendMail_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                string from = "webmaster@asp.net.vn";
                if (PortalSecurity.CurrentUser.Identity.IsAuthenticated) 
                    from = PortalSecurity.CurrentUser.Identity.Email;


                StringBuilder body = new StringBuilder();
                body.Append("Xin chao!<br><br>");
                body.AppendFormat("Toi tim thay thong tin nay tren <a href='{0}'>ASP.NET VIETNAM</a>. Toi nghi ban quan tam thong tin nay.<br><br>", Global.ApplicationPath);
                string url = HttpUrlBuilder.BuildUrl(this.ModuleID, this.ItemID, ModulePages.Attachment);
                url = url.Substring(2);

                body.AppendFormat("Xem chi tiet tai day: <a href='{0}'>{0}</a><br><br>", Global.ApplicationPath + "/" + url);

                body.AppendFormat("- Tieu de: <strong>{0}</strong><br><br>", lblTitle.Text);
                body.AppendFormat("- Thong diep: {0}<br><br>", txtMessage.Text);

                body.Append("<br>Chuc ban vui ve!");
                body.Append("<br><br><hr>");
                body.Append("<br><strong><a href='http://www.asp.net.vn'>ASP.NET VIET NAM</a> - Chia se kinh nghiem, nang cao ky nang.</strong>");

                string[] emails = txtEmails.Text.Split(';');
                foreach (string email in emails)
                {
                    MailHelper.SendMail(from, email, "", "ngothanhtung.it@gmail.com", "ASP.NET.VN - Thong tin chia se", body.ToString(), MailPriority.High);
                }
                txtEmails.Text = txtMessage.Text = string.Empty;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Comment comment = new Components.Comment();
                comment.PackageID = Convert.ToInt32(Request.QueryString["itemID"]);
                comment.Content = Server.HtmlEncode(txtContent.Text);
                comment.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                comment.CreationTime = DateTime.Now;
                comment.Insert();

                txtContent.Text = string.Empty;
                dlComments.DataBind();
            }
        }
    }
}